#!/bin/bash

client_file_path=$1
dbname=$2
dblogin=$3
dbpasswd=$4

function print_usage()
{
   echo "Usage: $0 <client file path> <dbname> <login> <passwd>"
}

function get_path()
{
    case "$1" in
	0)
	    echo "imported"
	    ;;
	
	8)
	    echo "skipped"
	    ;;
	
	*)
	    echo "error/"
	    ;;
    esac
}

if [ "x$dbpasswd" == "x" ]; then
   echo "`date +'%Y/%m/%d %H:%M:%S'` [ERROR] All arguments not available."
   print_usage
   exit 1
fi

lockfile="/tmp/import_keys.lock" 
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Checking lock file $lockfile..."
if [ -f $lockfile ]; then 
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Lock file exits! Aborting..." 
    exit
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Creating lock file..."
`touch /tmp/import_keys.lock` 

let "total_processed_file_counter=0"
let "skipped_processed_file_counter=0"
let "error_processed_file_counter=0"
let "new_processed_file_counter=0"

tmpfile1=/tmp/importkeys.tmp.1
tmpfile2=/tmp/importkeys.tmp.2

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Listing files from $client_file_path... "

find >$tmpfile1 $client_file_path -maxdepth 1 -name "*.txt" -o -name "K*.000"

C=`cat $tmpfile1 | wc -l`
if [ $C -eq 0 ]; then
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: No file found."
fi

cat $tmpfile1 | sed >$tmpfile2 s/\ [\ ]*/\+/g

for f in `cat $tmpfile2`
  do
  f_basename=`basename $f`
  f=`echo $f | sed s/\+/\ /g`
  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Processing file $f_basename... "

  let "total_processed_file_counter=$total_processed_file_counter+1"  
  /data/scripts/import_keys.pl --log /data/home/simgen/import_keys.log "$f" $dbname $dblogin $dbpasswd
  
  res=$?

  # res=0  -> process ok
  # res=1  -> function intro failed
  # res=2  -> function parse args failed
  # res=3  -> db connect failed
  # res=4  -> slsecd connect failed
  # res=5  -> lock file exists
  # res=6  -> lock file create failed
  # res=7  -> client file format not supported
  # res=8  -> client file already processed
  # res=9  -> event_id retrieve failed
  # res=10 -> event_id not expected value
  # res=11 -> data encipher failed
  # res=12 -> secure data update failed
  # res=13 -> function main failed
  # res=14 -> event insert failed when opl unknown
  
  filepath=`get_path $res`

  if [ $res -eq 5 -o $res -eq 4 -o $res -eq 3 -o $res -eq 6 -o $res -eq 1 -o $res -eq 2 -o $res -eq 13 ]; then
      let "error_processed_file_counter=$error_processed_file_counter+1"
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ERROR CODE=$res"
      break;

  elif [ $res -eq 8 ]; then
  let "skipped_processed_file_counter=$skipped_processed_file_counter+1"
  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File will be removed."
  `rm -f "$f"`

  elif [ $res -eq 7 -o $res -eq 9 -o $res -eq 10 -o $res -eq 11 -o $res -eq 12 -o $res -eq 14 ]; then
      let "error_processed_file_counter=$error_processed_file_counter+1"
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ERROR CODE=$res"
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be moved to $client_file_path$filepath..."
      `mv "$f" $client_file_path$filepath`
      
  elif [ $res -eq 0 ]; then
      let "new_processed_file_counter=$new_processed_file_counter+1"
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Done."
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be removed."
      #`rm -f "$f"`

  else
      let "error_processed_file_counter=$error_processed_file_counter+1"
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be moved to $client_file_path$filepath..."
      `mv "$f" $client_file_path$filepath`

  fi
  
done

rm -f $tmpfile1 $tmpfile2
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: End of Process."
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ### Summary: #################################################"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Total OPL file Processed: $total_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:         Included skipped: $skipped_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:           Included error: $error_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:             Included new: $new_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ##################################"

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Deleting lock file..."
`rm /tmp/import_keys.lock` 

error=`grep "\[ERROR\]" /tmp/import_keys.sh.log`
res=$?
if [ $res -eq 0 ]; then
    /data/scripts/sendmail.pl -f multisimgen.support@tcl.com -t raffi.semerciyan@jrdcom.com -s "Multisimgen >import_keys.sh< notification" /tmp/import_keys.sh.log
fi
